package mirabilis.org;

import java.util.ArrayList;
import java.util.ListIterator;

import random.CommonState;

import mirabilis.org.config.Configuration;
import mirabilis.org.individuals.Individual;
import mirabilis.org.individuals.real.DoubleChromosome;
import mirabilis.org.individuals.real.DoubleIndividual;
import mirabilis.samples.cec2014.Cec2014;



public abstract class Algorithm extends Thread{
	
	public static void main(String[] args) {
		Configuration.F=2;
		Configuration.chromosome_size=1;
		Configuration.range_max= 100;
		Configuration.range_min= -100;	
		Cec2014 bench = new Cec2014();
		
		
		double scaleratio = (Configuration.range_max - Configuration.range_min) - Math.abs(Configuration.range_min);
		double radius = (Configuration.range_max - Configuration.range_min)/2.0;
		
		double origin = -2;//CommonState.r.nextDouble()*scaleratio;
		double step = radius;
		double cum = 0;
		int dir = 1;//CommonState.r.nextBoolean()?-1:1;
		double[] genes = {origin};
		double fo = bench.evaluate(new DoubleIndividual(new DoubleChromosome(genes))); 
		//System.out.println(origin+" "+fo);
		
		double planetprima = Double.MAX_VALUE;
		
		for(int i=0;i<1000;i++){
			
			double planet;
			if((dir*step) +origin < Configuration.range_min){
				planet = Configuration.range_max - Math.abs(((dir*step) +origin)%Configuration.range_min);
			}else if ((dir*step) +origin > Configuration.range_max){
				planet = Configuration.range_min + ((dir*step) +origin)%Configuration.range_max;
			}else
				planet = (dir*step) +origin;
			
			
			double[] newplanet = {planet};
			double fplanet = bench.evaluate(new DoubleIndividual(new DoubleChromosome(newplanet)));
			
			if (planetprima < Double.MAX_VALUE)
				System.out.println(planetprima+" "+planet+" "+dir+" "+fplanet+" "+fo);
			else
				System.out.println(origin+" "+planet+" "+dir+" "+fplanet+" "+fo);
			
			if (fplanet <= fo){
				origin = planet;
				fo = fplanet;
				cum += step;
				step /= 2.0;
			}else{
				dir *= -1;
				step = (cum+step)/2.0;
				cum = 0;
			}
			//System.out.println(planet+" "+fplanet);
			
			planetprima = planet;
		}
		
	}
}
